clear *

use "${root}/data/processed/extended_final_sample.dta", clear

keep if extended_baseline_sample == 1

* baseline result (preferred definition of social spending, 2004-2016 period)
rdrobust res_social_exp_avg_share margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all
local coeff_avg : di %9.2f (e(tau_bc))
local se_avg : di %9.2f (e(se_tau_rb))

* second-best definition, baseline sample period (including pensions, 2004-2016 period)
rdrobust res_social_exp_p_avg_share margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all
local coeff_avg_p : di %9.2f (e(tau_bc))
local se_avg_p : di %9.2f (e(se_tau_rb))

* second-best definition, pre-2004 period (including pensions, 1996-2004 period)
rdrobust res_social_exp_p_avg_share margin_mayor_left if extended_baseline_sample==1 & year<2004, vce(cluster mun_code) all
local coeff_avg_p : di %9.2f (e(tau_bc))
local se_avg_p : di %9.2f (e(se_tau_rb))

* estimate effect by election cycle for extended period, using the second-best definition
foreach year in 1996 2000 2004 2008 2012 {	
	rdrobust res_social_exp_p_avg_share margin_mayor_left if year == `year', vce(cluster mun_code) all
	local coeff_p_`year' : di %9.2f (e(tau_bc))
	local se_p_`year' : di %9.2f (e(se_tau_rb))
	*local pv`i' : di %9.2f (e(pv_rb))
	*local n`i' = e(N)
	*local effn`i' = e(N_h_l) + e(N_h_r)
}

mat extended = (`coeff_p_1996', `se_p_1996' \ `coeff_p_2000', `se_p_2000' \ `coeff_p_2004', `se_p_2004' \ `coeff_p_2008', `se_p_2008' \ `coeff_p_2012', `se_p_2012')

mat list extended

mat extended_t = extended'
matrix colnames extended_t = "1996" "2000" "2004" "2008" "2012"
mat list extended_t

* estimate effect by election cycle for baseline period, using the first-best definition
foreach year in 2004 2008 2012 {	
	rdrobust res_social_exp_avg_share margin_mayor_left if year == `year' & baseline_sample==1, vce(cluster mun_code) all
	local coeff_`year' : di %9.2f (e(tau_bc))
	local se_`year' : di %9.2f (e(se_tau_rb))
}

mat baseline = ( . , . \ . , . \ `coeff_2004', `se_2004' \ `coeff_2008', `se_2008' \ `coeff_2012', `se_2012' )

mat list baseline

mat baseline_t = baseline'
matrix colnames baseline_t = "1996" "2000" "2004" "2008" "2012"
mat list baseline_t

* plot results
coefplot 	(matrix(baseline_t), se(2) color(black) ciopts(color(black) recast(rcap)) msymbol(o) msize(medlarge) label("Social spending")) ///
			(matrix(extended_t), se(2) color(blue)  ciopts(color(blue)  recast(rcap)) msymbol(d) msize(medlarge) label("Social spending + pensions")), ///
			vertical xtitle("Mayoral term") xlabel(1 "1997-2000" 2 "2001-2004" 3 "2005-2008" 4 "2009-2012" 5 "2013-2016") ytitle("Share of expenditure (p.p.)") yline(0, lpattern(dash)) scheme(plotplainblind) legend(rows(1) region(lwidth(none)) position(6)) /* ysize(4) xsize(6) */ name(all)
graph export "${root}/results/figures/social_exp_extended.pdf", name("all") replace
